package com.lishe.base.common.base;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;


/**
 * Created by lwa on 2017/8/9.
 * fragment基类
 */

public class BaseFragment extends Fragment implements View.OnTouchListener {

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        return false;
    }

    public Context context;
    private ProgressDialog dialog;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        context = getActivity();
        //        getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); //强制为横屏
        getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);       //强制为竖屏
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    Toast toast;

    public void toast(String text) {
        if (isAdded() && text != null) {
            if (toast == null) {
                toast = Toast.makeText(context,"",Toast.LENGTH_SHORT);
            }
            toast.setText(text);
            toast.show();
        }
    }

    public void toast(int resId) {
        if (isAdded()) {
            if (toast == null) {
                toast = Toast.makeText(context,"",Toast.LENGTH_SHORT);
            }
            toast.setText(getString(resId));
            toast.show();
        }
    }


    /**
     * @param container
     * @param target
     */
    public void jumpReplaceFragment(int container, Fragment target) {
        FragmentManager fm = getActivity().getSupportFragmentManager();
        fm.beginTransaction().replace(container, target).addToBackStack(null).commitAllowingStateLoss();
    }

    public void jumpAddFragment(int container, BaseFragment target) {
        FragmentManager fm = getActivity().getSupportFragmentManager();
        fm.beginTransaction().add(container, target).addToBackStack(null).commitAllowingStateLoss();
    }

    /**
     * @param container
     * @param target
     */
    public void jumpFragmentByAdd(int container, BaseFragment target, String tag) {
        FragmentManager fm = getActivity().getSupportFragmentManager();
        fm.beginTransaction().add(container, target, tag).addToBackStack(null).commitAllowingStateLoss();
    }


    /**
     * 处理回退事件
     */
    public void onBackPressed() {
        getActivity().getSupportFragmentManager().popBackStack();
    }

    public void getDefaultProgressDialogs(String msg, boolean cancelable) {
        dialog = new ProgressDialog(context);
        dialog.setMessage(msg);
        dialog.setCancelable(cancelable);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                dialog = null;
            }
        });
        dialog.show();
    }

    public boolean dismissProgressDialogs() {
        if (dialog != null) {
            dialog.dismiss();
            dialog = null;
            return true;
        }
        return false;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    public boolean findFragmentByJumpTagAndRemove(String tag) {
        Fragment fragment = getActivity().getSupportFragmentManager().findFragmentByTag(tag);
        if (fragment != null) {
            getActivity().getSupportFragmentManager().beginTransaction().remove(fragment).commit();
            return true;
        } else {
            return false;
        }
    }

    public void callBack(Activity activity) {
        activity.getFragmentManager().popBackStack();
    }
}
